package com.lx.huimail.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.lx.huimail.R;
import com.lx.huimail.adapter.OrderManagerAdapter;
import com.lx.huimail.adapter.OrderManagerAdapter1;
import com.lx.huimail.configs.Config;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;

public class ChaoshiActivity extends Activity {
    RelativeLayout fanhui;
    RecyclerView recyclerView;
    private String custid;
    public static Activity instance;
    private OrderManagerAdapter1 orderManagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chaoshi);
        instance=this;
        SharedPreferences sp=getSharedPreferences("user", Context.MODE_PRIVATE);
        custid=sp.getString("custid","-11111");
        fanhui=(RelativeLayout)findViewById(R.id.fanhui);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChaoshiActivity.this.finish();
            }
        });
        recyclerView=(RecyclerView) findViewById(R.id.recyclerview);
        orderTimeOut();
    }
    private void orderTimeOut(){
        HashMap<String,String> map=new HashMap<String,String>();
        map.put("senderid",custid);
        map.put("page","1");
        map.put("rows","20");
        Gson gson=new Gson();
        String str=gson.toJson(map).toString();
        OkHttpUtils
                .post()
                .url(Config.url+"order/OrderTimeOut.do")
                .addParams("data",str)
                .addParams("mobile","true")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        System.out.print("失败=="+e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.print("超时订单=="+response);
                        JSONArray jsonArray;
                        JSONObject jsonObject;
                        ArrayList<HashMap<String,String>> mList=new ArrayList<HashMap<String, String>>();
                        try {
                            jsonArray=new JSONArray(response);
                            for (int i=0;i<jsonArray.length();i++){
                                HashMap<String,String> map=new HashMap<String, String>();
                                map.put("orderno",jsonArray.getJSONObject(i).getString("orderno"));
                                map.put("warntime",jsonArray.getJSONObject(i).getString("warntime"));
                                map.put("sendstatus",jsonArray.getJSONObject(i).getString("sendstatus"));
                                map.put("ordercount",jsonArray.getJSONObject(i).getString("ordercount"));
                                map.put("ordermoney",jsonArray.getJSONObject(i).getString("ordermoney"));
                                map.put("custname",jsonArray.getJSONObject(i).getString("custname"));
                                map.put("receiveraddr",jsonArray.getJSONObject(i).getString("receiveraddr"));
                                map.put("warnflag",jsonArray.getJSONObject(i).getString("warnflag"));
                                map.put("custname",jsonArray.getJSONObject(i).getString("custname"));
                                map.put("name",jsonArray.getJSONObject(i).getString("sendername"));
                                map.put("receiveraddr",jsonArray.getJSONObject(i).getString("receiveraddr"));
                                map.put("online1",jsonArray.getJSONObject(i).getString("online1"));
                                map.put("array",String.valueOf(jsonArray.getJSONObject(i).getJSONArray("prolist")));
                                mList.add(map);

                            }
                            orderManagerAdapter=new OrderManagerAdapter1(ChaoshiActivity.this,mList);
                            recyclerView.setLayoutManager(new LinearLayoutManager(ChaoshiActivity.this));
                            recyclerView.setAdapter(orderManagerAdapter);
                            orderManagerAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
